package edu.utsa.cs.contactmanager.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import edu.utsa.cs.contactmanager.Contact;
import edu.utsa.cs.contactmanager.ContactManager;
import edu.utsa.cs.contactmanager.R;

public class View_SearchResults extends ListActivity{
	
	private ArrayList<Long> cids, gids;
	private ArrayList<String> names;
    private HashMap<String, Contact> hm;
	
	private String searchString = "";
	private ArrayList<Contact> contactsFound;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_search_results);
	}
	
	//TODO list the groups found too, they are in gids, everything has been implemented.
	private void resetView(){
		
		Intent i = getIntent();
        cids = (ArrayList<Long>)i.getSerializableExtra("contacts");
        gids = (ArrayList<Long>)i.getSerializableExtra("groups");
        searchString = (String)i.getSerializableExtra("searchString");
	    
		names = new ArrayList<String>();
        hm = new HashMap<String, Contact>();
                
	    contactsFound = ContactManager.getContacts(cids.toArray(new Long[0]));

        if(contactsFound == null || contactsFound.isEmpty()){
        	setContentView(R.layout.activity_view_search_results);
            TextView t = (TextView)findViewById(R.id.title);
            t.setText("Search for \"" + searchString + "\" did not match anything. Try a different keyword.");
        }
        else{
            String name;
            
            Iterator<Contact> iter = contactsFound.iterator();
            Contact contact;
            
            while(iter.hasNext()){
            	contact = iter.next();
                name = contact.getFirstName() + " " + contact.getLastName();
                names.add(name);
                hm.put(name, contact);
            }
            
            setContentView(R.layout.activity_view_search_results);
            TextView t = (TextView)findViewById(R.id.title);
            t.setText("Search results for: \"" + searchString + "\"");
            setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names));
        }
        //Display list adapter
	}
	
	@Override
    protected void onListItemClick(ListView list, View view, int position, long id){
        super.onListItemClick(list, view, position, id);
        Contact contact = hm.get(names.get(position));
        
        Intent i = new Intent(this, View_SingleContact.class);
        i.putExtra("contact", contact);
        startActivity(i);
    }
	
	@Override
    public void onResume(){
    	ContactManager.open();
    	resetView();
    	super.onResume();
    };
    
    @Override
    public void onPause(){
    	ContactManager.close();
    	super.onPause();
    }
    
}
